<template>
  <div class="app-container">
    <div style="padding:20px;">
      <el-form :inline="true" class="search-form" label-width="90px">
        <el-row style="display: flex; flex-wrap: wrap;margin-top: 5px;">
          <el-col :span="20">
            <el-form-item label="活动名称：">
              <el-input
                placeholder="请输入名称"
                v-model="searchForm.activity_name"
                maxlength="20"
                show-word-limit
                onkeyup="value =value.replace(/[^a-zA-Z0-9\u4e00-\u9fa5]/g, '')"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="状态：">
              <el-select v-model="searchForm.status" placeholder="请选择状态">
                <el-option
                  v-for="item in [
                    { name: '结束', id: '-1' },
                    { name: '已开始', id: '1' },
                    { name: '已关闭', id: '2' },
                    { name: '进行中', id: '3' },
                  ]"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="text-align: right">
            <el-form-item>
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="getListWay"
                >查询</el-button
              >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-refresh"
                @click="resetting"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-divider></el-divider>
    <div class="marginTop">
      <div class="marginBottom">
        <el-row :gutter="20">
          <el-col>
            <el-button
              icon="el-icon-circle-plus-outline"
              type="primary"
              size="small"
              @click="addWay('/activity/agriculture/save')"
              >添加活动</el-button
            >
          </el-col>
        </el-row>
      </div>
      <el-row :gutter="20">
        <el-col>
          <el-table
            :header-cell-style="{
              'background-color': '#f3f8fe',
              color: '#606266',
              height: '50px',
            }"
            v-loading="loading"
            highlight-current-row
            :data="dataList"
            style="width: 100%"
          >
            <el-table-column align="center" prop="id" label="活动编号">
            </el-table-column>
            <el-table-column
              align="center"
              prop="activity_name"
              label="活动名称"
            >
            </el-table-column>
            <el-table-column align="center" prop="start_time" label="开始时间">
            </el-table-column>
            <el-table-column align="center" prop="end_time" label="结束时间">
            </el-table-column>
            <el-table-column align="center" prop="areaArray" label="生效区域">
              <template slot-scope="scope">
                <div v-if="scope.row.areaArray.length !== 0">
                  <span
                    style="margin: 5px"
                    v-for="(item, index) in scope.row.areaArray"
                    :key="index"
                    >{{ item.short_name }}</span
                  >
                </div>
                <span v-else type="danger">无</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" label="商品">
              <template slot-scope="scope">
                <el-button @click="lookGoodsWay(scope.row)" type="text"
                  >查看活动商品</el-button
                >
              </template>
            </el-table-column>
            <el-table-column align="center" prop="status" label="状态">
              <template slot-scope="scope">
                <span v-if="scope.row.status == -1" style="color:#ff4949;"
                  >活动结束</span
                >
                <span v-if="scope.row.status == 1" style="color:#13ce66;"
                  >已开启</span
                >
                <span v-if="scope.row.status == 2" style="color:#81b337;"
                  >已关闭</span
                >
                <span v-if="scope.row.status == 3" style="color:#13ce66;"
                  >进行中</span
                >
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              fixed="right"
              width="300"
              prop="address"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  style="margin: 5px"
                  v-if="scope.row.status == 1 || scope.row.status == 3"
                  type="warning"
                  size="mini"
                  @click="saveStatus(scope.row, 2)"
                >
                  关闭活动
                </el-button>
                <el-button
                disabled
                  style="margin: 5px"
                  v-if="scope.row.status == -1"
                  type="danger"
                  size="mini"
                >
                  活动结束
                </el-button>
                <el-button
                  style="margin: 5px"
                  v-if="scope.row.status == 2"
                  type="success"
                  size="mini"
                  @click="saveStatus(scope.row, 1)"
                >
                  开启活动
                </el-button>
                <el-button
                  style="margin: 5px"
                  type="primary"
                  size="mini"
                  @click="detailsDrawerWay(scope.row)"
                >
                  详情
                </el-button>
                <el-button
                  :disabled="scope.row.status == 3 || scope.row.status == 1"
                  style="margin: 5px"
                  type="primary"
                  size="mini"
                  @click="
                    addWay('/activity/agriculture/save?id=' + scope.row.id)
                  "
                >
                  修改
                </el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @confirm="delWay(scope.row)"
                >
                  <el-button
                    :disabled="scope.row.status == 3 || scope.row.status == 1"
                    style="margin: 5px"
                    type="danger"
                    size="mini"
                    slot="reference"
                  >
                    删除
                  </el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row class="marginTop" style="text-align: right">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchForm.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="searchForm.page.count"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="800px"
      @close="handleClose"
    >
      <el-row :gutter="20" v-loading="submitLoading">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="110px"
          class="demo-ruleForm"
        >
          <el-col :span="24" class="splitTitle">
            <div class="split">活动信息</div>
          </el-col>
          <el-col :span="11">
            <el-form-item label="活动名称" prop="activity_name">
              <el-input
                placeholder="请输入活动名称"
                v-model="ruleForm.activity_name"
                maxlength="20"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="时间段" prop="domeTime">
              <el-date-picker
                @change="valueFormaChange"
                value-format="yyyy-MM-dd"
                v-model="domeTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="省市区" prop="cascaderRegList">
              <el-cascader
                v-model="cascaderRegList"
                style="width: 100%"
                :options="regionList"
                collapse-tags
                :props="{
                  multiple: true,
                  value: 'id',
                  label: 'name',
                  children: 'children',
                  checkStrictly: true,
                }"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="活动排序" prop="sort">
              <el-input
                placeholder="请输入活动排序"
                v-model="ruleForm.sort"
                maxlength="20"
                show-word-limit
                onkeyup="value=value.replace(/\D/g,'')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="活动轮播" prop="images">
              <logo-upload
                :file-list="images"
                :show-file-list="true"
                accept="image/jpeg, image/png"
                @uploadCallback="carouselBack"
              />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="活动说明" prop="explain">
              <el-input
                placeholder="请输入活动说明"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="ruleForm.explain"
                maxlength="200"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="splitTitle">
            <div class="split">活动规则</div>
          </el-col>
          <el-col :span="22">
            <el-form-item label="参与类型" prop="p_goods_type">
              <el-radio-group v-model="ruleForm.p_goods_type" size="mini">
                <el-radio :label="1">商户商品</el-radio>
                <el-radio :label="2">商户</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="ruleForm.p_goods_type == 1">
            <el-form-item label="参与活动商品" prop="goods_ids">
              <el-select
                multiple
                v-model="goods_ids"
                style="width: 100%"
                filterable
                remote
                reserve-keyword
                placeholder="关键字查询支持商品名、ID、副标题、编码"
                :remote-method="getGoodsListWay"
                :loading="goodsLoading"
              >
                <el-option
                  v-for="item in GoodsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="ruleForm.p_goods_type == 2">
            <el-form-item label="参与活动商户" prop="goods_ids">
              <el-select
                multiple
                v-model="goods_ids"
                style="width: 100%"
                filterable
                remote
                reserve-keyword
                placeholder="关键字查询支持参与活动商户"
                :remote-method="getGoodsListWay"
                :loading="goodsLoading"
              >
                <el-option
                  v-for="item in GoodsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="参与用户" prop="p_goods_type">
              <el-radio-group v-model="ruleForm.p_user_type" size="mini">
                <el-radio :label="1">全部</el-radio>
                <el-radio :label="2">指定用户</el-radio>
                <el-radio :label="3">指定用户等级</el-radio>
                <el-radio :label="4">指定用户标签</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="ruleForm.p_user_type == 2">
            <el-form-item label="指定用户" prop="goods_ids">
              <el-select
                multiple
                v-model="goods_ids"
                style="width: 100%"
                filterable
                remote
                reserve-keyword
                placeholder="关键字查询支持商品名、ID、副标题、编码"
                :remote-method="getGoodsListWay"
                :loading="goodsLoading"
              >
                <el-option
                  v-for="item in GoodsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="ruleForm.p_user_type == 3">
            <el-form-item label="指定用户等级" prop="goods_ids">
              <el-select
                multiple
                v-model="goods_ids"
                style="width: 100%"
                filterable
                remote
                reserve-keyword
                placeholder="关键字查询支持参与活动商户"
                :remote-method="getGoodsListWay"
                :loading="goodsLoading"
              >
                <el-option
                  v-for="item in GoodsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22" v-if="ruleForm.p_user_type == 4">
            <el-form-item label="指定用户标签" prop="goods_ids">
              <el-select
                multiple
                v-model="goods_ids"
                style="width: 100%"
                filterable
                remote
                reserve-keyword
                placeholder="关键字查询支持参与活动商户"
                :remote-method="getGoodsListWay"
                :loading="goodsLoading"
              >
                <el-option
                  v-for="item in GoodsList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="22">
            <el-form-item label="是否开启奖励" prop="consume_status">
              <el-radio-group v-model="ruleForm.consume_status" size="mini">
                <el-radio :label="0">关闭</el-radio>
                <el-radio :label="1">积分</el-radio>
                <el-radio :label="2">提货券</el-radio>
                <el-radio :label="3">余额</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="11" v-if="ruleForm.consume_status !== 0">
            <el-form-item label="消费金额" prop="consume_price">
              <el-input
                placeholder="请输入消费金额"
                v-model="ruleForm.consume_price"
                maxlength="10"
                show-word-limit
                onkeyup="value=value.replace(/\D/g,'')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11" v-if="ruleForm.consume_status !== 0">
            <el-form-item label="奖励金" prop="consume_award_price">
              <el-input
                placeholder="请输入奖励金"
                v-model="ruleForm.consume_award_price"
                maxlength="10"
                show-word-limit
                onkeyup="value=value.replace(/\D/g,'')"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="备注" prop="remark">
              <el-input
                placeholder="请输入备注"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="ruleForm.remark"
                maxlength="200"
                show-word-limit
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      title="查看商品"
      :visible.sync="goodsLookDialog"
      width="800px"
    >
      <el-row :gutter="20">
        <el-table
          :header-cell-style="{
            'background-color': '#f3f8fe',
            color: '#606266',
            height: '50px',
          }"
          v-loading="goodsLookLoading"
          highlight-current-row
          :data="GoodsDataList"
          style="width: 100%"
        >
          <el-table-column align="center" prop="id" label="id">
          </el-table-column>
          <el-table-column align="center" prop="name" label="商品名称">
          </el-table-column>
          <el-table-column align="center" prop="main_image" label="主图">
            <template slot-scope="scope">
              <el-image
                style="width: 50px"
                v-if="scope.row.main_image"
                :src="scope.row.main_image"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="subtitle" label="副标题">
          </el-table-column>
        </el-table>
      </el-row>
    </el-dialog>

    <!-- 活动详情 -->
    <el-drawer
      title="活动详情"
      :wrapperClosable="true"
      size="800px"
      :visible.sync="detailsDrawer"
    >
      <el-row
        style="padding: 0 30px 30px; position: relative"
        v-loading="detailsLoading"
      >
        <el-descriptions title="基本信息" :column="3" border>
          <el-descriptions-item>
            <template slot="label"> 活动名称 </template>
            {{ detailsObj.activity_name ? detailsObj.activity_name : "-" }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 创建时间 </template>
            {{ detailsObj.created_at ? detailsObj.created_at : "-" }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 最新更改时间 </template>
            {{ detailsObj.updated_at ? detailsObj.updated_at : "-" }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 开始时间 </template>
            {{ detailsObj.start_time ? detailsObj.start_time : "-" }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 结束时间 </template>
            {{ detailsObj.end_time ? detailsObj.end_time : "-" }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label"> 排序 </template>
            {{ detailsObj.sort ? detailsObj.sort : "-" }}
          </el-descriptions-item>
          <el-descriptions-item :span="3">
            <template slot="label"> 活动轮播图 </template>
            <div
              v-for="(item, index) in detailsObj.image"
              :key="index"
              style="
                display: inline-block;
                width: 40px;
                height: 40px;
                margin: 5px;
                overflow: hidden;
              "
            >
              <el-image
                style="width: 100%; height: 100%"
                fit="cover"
                :src="item"
                :preview-src-list="detailsObj.image"
              />
            </div>
          </el-descriptions-item>
          <el-descriptions-item :span="3">
            <template slot="label"> 活动说明 </template>
            {{ detailsObj.explain ? detailsObj.explain : "-" }}
          </el-descriptions-item>
          <el-descriptions-item :span="3">
            <template slot="label"> 备注 </template>
            {{ detailsObj.remark ? detailsObj.remark : "-" }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions
          title="规则信息"
          style="margin-top: 40px"
          :column="3"
          border
        >
          <el-descriptions-item :span="3">
            <template slot="label"> 地区限制 </template>
            <div
              v-if="
                detailsObj.areaArray ? detailsObj.areaArray.length !== 0 : false
              "
            >
              <el-tag
                style="margin: 5px"
                size="mini"
                v-for="(items, index) in detailsObj.areaArray
                  ? detailsObj.areaArray
                  : []"
                :key="index"
                >{{ items.short_name }}</el-tag
              >
            </div>

            <el-tag v-else size="mini" style="margin: 5px">无限制</el-tag>
          </el-descriptions-item>

          <el-descriptions-item :span="3">
            <template slot="label"> 活动参与类型 </template>
            <el-tag v-if="detailsObj.p_goods_type == 2" size="mini"
              >指定部分商户</el-tag
            >
            <el-tag v-else size="mini">指定部分商品</el-tag>
          </el-descriptions-item>
          <el-descriptions-item :span="3" v-if="detailsObj.p_goods_type == 2">
            <template slot="label"> 商户名称 </template>
            <el-tag
              style="margin: 5px"
              v-for="(item, index) in detailsObj.merchant"
              :key="index"
              size="mini"
              >{{ item.name }}</el-tag
            >
          </el-descriptions-item>
          <el-descriptions-item :span="3" v-else>
            <template slot="label"> 产品名称 </template>
            <el-tag
              style="margin: 5px"
              v-for="(item, index) in detailsObj.merchant"
              :key="index"
              size="mini"
              >{{ item.name }}</el-tag
            >
          </el-descriptions-item>
          <el-descriptions-item :span="3">
            <template slot="label"> 参加活动限制 </template>
            <el-tag v-if="detailsObj.p_user_type == 1" size="mini"
              >不限制用户</el-tag
            >
            <el-tag v-if="detailsObj.p_user_type == 2" size="mini"
              >指定用户</el-tag
            >
            <el-tag v-if="detailsObj.p_user_type == 3" size="mini"
              >指定用户等级</el-tag
            >
            <el-tag v-else size="mini">指定用户标签</el-tag>
          </el-descriptions-item>
          <el-descriptions-item :span="3" v-if="detailsObj.p_user_type == 2">
            <template slot="label"> 指定用户 </template>
            <el-tag
              style="margin: 5px"
              v-for="(item, index) in detailsObj.userInfo"
              :key="index"
              size="mini"
              >{{ item.name }}</el-tag
            >
          </el-descriptions-item>
          <el-descriptions-item :span="3" v-if="detailsObj.p_user_type == 3">
            <template slot="label"> 指定等级 </template>
            <el-tag
              style="margin: 5px"
              v-for="(item, index) in detailsObj.userInfo"
              :key="index"
              size="mini"
              >{{ item.name }}</el-tag
            >
          </el-descriptions-item>
          <el-descriptions-item :span="3" v-if="detailsObj.p_user_type == 4">
            <template slot="label"> 指定标签 </template>
            <el-tag
              style="margin: 5px"
              v-for="(item, index) in detailsObj.userInfo"
              :key="index"
              size="mini"
              >{{ item.name }}</el-tag
            >
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions
          v-for="(item, index) in detailsObj.ruleList"
          :key="index"
          title="推荐好物规则"
          style="margin-top: 40px"
          :column="4"
          border
        >
          <el-descriptions-item :span="2">
            <template slot="label"> 数额范围 </template>
            <el-tag style="margin: 5px" size="mini"
              >{{ item.section_end }} - {{ item.section_start }}</el-tag
            >
          </el-descriptions-item>
          <el-descriptions-item :span="2">
            <template slot="label"> 积分奖励数额 </template>
            {{ item.integral }}
          </el-descriptions-item>
          <el-descriptions-item :span="4">
            <template slot="label"> 推荐商品 </template>
            <el-table :data="item.goods_list" style="width: 100%">
              <el-table-column align="center" prop="id" label="商品ID">
              </el-table-column>
              <el-table-column align="center" prop="name" label="商品名称">
              </el-table-column>
              <el-table-column align="center" prop="main_image" label="图片">
                <template slot-scope="scope">
                  <div
                    style="
                      display: inline-block;
                      width: 40px;
                      height: 40px;
                      margin: 5px;
                      overflow: hidden;
                    "
                  >
                    <el-image
                      style="width: 100%; height: 100%"
                      fit="cover"
                      :src="scope.row.main_image"
                      :preview-src-list="[scope.row.main_image]"
                    />
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </el-descriptions-item>
        </el-descriptions>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
import {
  getLoveFarmerList,
  SaveLoveFarmer,
  getLoveFarmerGoodsList,
  deleteLoveFarmerGoods,
  editStatusFarmerGoods,
  detailFarmerGoods,
} from "@/api/activity/agriculture";
import { getRegionList } from "@/api/user/index";
import { getActivityGoodsList } from "@/api/index";
import LogoUpload from "@/components/FileUpload";
export default {
  components: { LogoUpload },
  data() {
    return {
      loading: false,
      searchForm: {
        activity_name: "",
        count: 10,
        page: 1,
        status: "",
      },
      dataList: [],
      total: 0,
      // 省市区列表
      regionList: [],
      title: "添加活动",
      dialogVisible: false,
      ruleForm: {
        id: "",
        sort: 0,
        activity_name: "",
        // status: 2,
        explain: "",
        consume_status: 0,
        consume_price: 0,
        consume_award_price: 0,
        remark: "",
        image: "",
        area_ids_json: "",
        start_time: "",
        end_time: "",
        goods_ids: [],
      },
      submitLoading: false,
      // 时间段回显
      domeTime: [],
      // 省市区回显
      cascaderRegList: [],
      // 活动轮播回显
      images: [],
      // 活动商品回显
      goods_ids: [],
      rules: {
        activity_name: [
          {
            required: true,
            validator: (rule, value, callback) => {
              let req = /[^0-9a-zA-Z\u4e00-\u9fa5]/g;
              if (!value) {
                callback(new Error("请输入活动名称"));
              } else {
                if (req.test(value)) {
                  callback(new Error("活动名称中不能存在特殊字符"));
                } else {
                  callback();
                }
              }
            },
            trigger: "change",
          },
        ],
        domeTime: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.domeTime.length == 0) {
                return callback(new Error("请选择活动的时间段"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        goods_ids: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.goods_ids.length == 0) {
                return callback(new Error("请选择参加活动的商品"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        explain: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入活动说明"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        images: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.images.length == 0) {
                return callback(new Error("请上传轮播图片"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        consume_price: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.ruleForm.consume_status !== 0) {
                if (!value) {
                  return callback(new Error("请输入消费金额"));
                } else {
                  callback();
                }
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value == "") {
                return callback(new Error("请输入序号,序号越大越靠前"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        consume_award_price: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.ruleForm.consume_status !== 0) {
                if (!value) {
                  return callback(new Error("请输入奖励金"));
                } else {
                  callback();
                }
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
      goodsSaerch: {
        keywords: "",
        page: 1,
        count: 20,
      },
      GoodsList: [],
      goodsLoading: false,
      goodsLookLoading: false,
      goodsLookDialog: false,
      GoodsDataList: [],
      disabledGoods: true,
      detailsDrawer: false,
      detailsLoading: false,
      detailsObj: {},
    };
  },
  created() {
    getRegionList().then((res) => {
      this.regionList = res.data;
    });
    this.getListWay();
  },
  methods: {
    getListWay() {
      this.loading = true;
      getLoveFarmerList(this.searchForm)
        .then((res) => {
          this.loading = false;
          this.dataList = res.data.list;
          this.total = res.data.total;
        })
        .catch((err) => {
          this.loading = false;
          this.$messages.error(err.msg);
        });
    },
    resetting() {
      this.searchForm = {
        activity_name: "",
        count: 10,
        page: 1,
        status: "",
      };
      this.getListWay();
    },
    addWay(url) {
      this.$router.push(url);
      // this.handleClose();
      // this.title = "添加活动";
      // this.dialogVisible = true;
    },
    detailsDrawerWay(row) {
      this.detailsDrawer = true;
      this.detailsLoading = true;
      detailFarmerGoods({ id: row.id }).then((res) => {
        if (res.code !== 200 || res.status !== 1) {
          this.$messages.error(res.msg);
          this.detailsLoading = false;
          return;
        }
        res.data.image = res.data.image.split(",");
        this.detailsObj = res.data;
        this.detailsLoading = false;
        console.log(this.detailsObj);
      });
    },
    delWay(row) {
      this.loading = true;
      deleteLoveFarmerGoods({ id: row.id })
        .then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            this.loading = false;
            return;
          }
          this.$messages.success("删除成功");
          this.getListWay();
        })
        .catch((err) => {
          this.loading = false;
          this.$messages.error(err.msg);
        });
    },
    emitWay(row) {
      this.dialogVisible = true;
      this.submitLoading = true;
      detailFarmerGoods({ id: row.id })
        .then((res) => {
          this.goods_ids = [];
          res.data.goods_list.forEach((item) => {
            this.goods_ids.push(item.id);
          });
          this.GoodsList = [];
          this.GoodsList.push(...res.data.goods_list);
          this.ruleForm.activity_name = res.data.activity_name;
          this.ruleForm.sort = Number(res.data.sort);
          // this.ruleForm.status = Number(res.data.status);
          this.domeTime = [res.data.start_time, res.data.end_time];
          this.cascaderRegList = JSON.parse(res.data.area_ids_json);
          this.ruleForm.explain = res.data.explain;
          this.ruleForm.consume_status = res.data.consume_status;
          this.ruleForm.consume_price = res.data.consume_price;
          this.ruleForm.consume_award_price = res.data.consume_award_price;
          this.ruleForm.remark = res.data.remark;
          this.ruleForm.id = res.data.id;
          this.images = [];
          var arr = res.data.image.split(",");
          arr.forEach((item) => {
            this.images.push({
              url: item,
            });
          });
          this.disabledGoods = false;
          this.submitLoading = false;
          this.$forceUpdate();
        })
        .catch((err) => {
          this.submitLoading = false;
          this.dialogVisible = false;
          this.$messages.error(err.msg);
        });
    },
    saveStatus(row, init) {
      var obj = {
        id: row.id,
        status: init,
      };
      this.loading = true;
      editStatusFarmerGoods(obj)
        .then((res) => {
          this.$messages.success(res.msg);
          this.getListWay();
        })
        .catch((err) => {
          this.$messages.error(err.msg);
          this.loading = false;
        });
    },
    valueFormaChange(val) {
      console.log(val);
      if (val !== null) {
        this.disabledGoods = false;
      } else {
        this.disabledGoods = true;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var arr = [];
          this.images.forEach((item) => {
            arr.push(item.url);
          });
          this.ruleForm.start_time = this.domeTime[0];
          this.ruleForm.end_time = this.domeTime[1];
          this.ruleForm.image = arr.toString();
          this.ruleForm.goods_ids = this.goods_ids.toString();
          if (this.cascaderRegList.length !== 0) {
            this.ruleForm.area_ids_json = JSON.stringify(this.cascaderRegList);
          }
          SaveLoveFarmer(this.ruleForm)
            .then((res) => {
              this.handleClose();
              this.dialogVisible = false;
              this.submitLoading = false;
              this.$messages.success("操作成功");
              this.getListWay();
            })
            .catch((res) => {
              this.$messages.error(res.msg);
              this.submitLoading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose() {
      this.ruleForm = {
        id: "",
        sort: 0,
        activity_name: "",
        // status: 2,
        explain: "",
        consume_status: 0,
        consume_price: 0,
        consume_award_price: 0,
        remark: "",
        image: "",
        area_ids_json: "",
        start_time: "",
        end_time: "",
        goods_ids: [],
      };
      this.domeTime = [];
      this.cascaderRegList = [];
      this.images = [];
      this.goods_ids = [];
    },
    getGoodsListWay(query) {
      this.goodsLoading = true;
      if (query !== "") {
        this.goodsSaerch.keywords = query;
      }
      var obj = {
        ...this.goodsSaerch,
        action: "love",
        start_time: this.domeTime[0],
        end_time: this.domeTime[1],
      };
      getActivityGoodsList(obj)
        .then((res) => {
          this.goodsLoading = false;
          this.GoodsList = res.data.list;
          this.goodsTotal = res.data.total;
        })
        .catch((err) => {
          this.$messages.error(err.msg);
          this.goodsLoading = false;
        });
    },
    // 查看商品列表
    lookGoodsWay(row) {
      this.goodsLookDialog = true;
      this.goodsLookLoading = true;
      getLoveFarmerGoodsList({ activity_id: row.id })
        .then((res) => {
          this.GoodsDataList = res.data;
          this.goodsLookLoading = false;
        })
        .catch((err) => {
          this.goodsLookLoading = false;
          this.goodsLookDialog = false;
          this.$messages.error(err.msg);
        });
    },
    handleSizeChange(val) {
      this.searchForm.count = val;
      this.getListWay();
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.getListWay();
    },
    carouselBack() {},
  },
};
</script>

<style lang="scss" scoped>
.splitTitle {
  font-size: 16px;
  font-weight: 700;
  color: #606266;

  .split {
    width: 95%;
    border-bottom: 2px solid #c9c9c980;
    padding-bottom: 10px;
    margin: 20px auto;
  }
}
</style>
<style>
.el-upload--picture-card {
  width: 90px !important;
  height: 90px !important;
  line-height: 90px;
}

.el-upload-list--picture-card .el-upload-list__item-actions {
  width: 90px !important;
  height: 90px !important;
}

.el-upload-list--picture-card .el-upload-list__item-thumbnail {
  width: 90px !important;
  height: 90px !important;
}

.el-upload-list--picture-card .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
}

.tox .tox-dialog-wrap__backdrop {
  z-index: 9993 !important;
}

.tox-tinymce-aux {
  z-index: 9990 !important;
}

.tox .tox-dialog {
  z-index: 9995 !important;
}
</style>
